<?php

namespace App\Service\UserCenter\v1_0_8;

use App\Model\ProfileVerifyLog;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Service\BaseService;

/**
 * 是否已经认证通过
 */
class CheckVerfiedService extends BaseService
{
    public function handle()
    {
        $version = $this->request->input('version', '1.0.0');
        $user = $this->getUser();
        $personInfo = UserProfilePersonal::getProfile($this->userId);
        $realCheck = UserProfilePersonal::isFaceVerified($personInfo);
        $phone = $user->phone ?: '';
        $ret = [
            'result' => $realCheck ? 1 : 0,
            'phone' => $phone,
            'real_check' => $realCheck,
        ];
        //新版本
        if ($version >= '1.0.0') {
            $ret['real_avatar_check'] = UserProfile::checkAvatarVrfied($this->userId);
        }

        //新版本+微信认证
        if ($version >= '1.0.0') {
            $log = ProfileVerifyLog::checkVerified($this->userId, ProfileVerifyLog::TYPE_WECHAT, 'data');
            $ret['wechat_check'] = $this->getVryStatus($log);
        }
        log_user_result($this->userId, $ret);
        return $ret;
    }

    /**
     * 认证状态
     */
    function getVryStatus($log)
    {
        //已认证
        if ($log && $log->status == ProfileVerifyLog::STATUS_OK) return 1;
        //审核中
        elseif ($log && $log->status == ProfileVerifyLog::STATUS_ING) return 2;
        return 0;
    }
}
